/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AltaFuncionPelicula.java
 *
 * Created on 19-jun-2011, 18:59:51
 */
package guiAdministracion;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import logica.Fabrica;
import logica.IAdministracion;
import logica.IKiosco;
import util.datatypes.DTFuncion;
import util.datatypes.DTPelicula;
import util.exceptions.NoExistePeliculaException;

/**
 *
 * @author Ernesto
 */
public class AltaFuncionPelicula extends javax.swing.JFrame {

    public AltaFuncionPelicula() {
        initComponents();

        IKiosco ik = Fabrica.getIKiosco();
        List<DTPelicula> peliculas = ik.listarPeliculas();

        for (DTPelicula dt : peliculas) {
            cmb.addItem(dt);
        }

        DefaultListModel dlm = new DefaultListModel();

        for (DTPelicula dt : peliculas) {

            dlm.addElement(dt);
        }
        lstPeliculas.setModel(dlm);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        btnAgregar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstPeliculas = new javax.swing.JList();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        cmb = new javax.swing.JComboBox();
        btnActualizar = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jPanel8 = new javax.swing.JPanel();
        txtInicio = new javax.swing.JFormattedTextField();
        txtFin = new javax.swing.JFormattedTextField();
        txtNroEntradas = new javax.swing.JTextField();
        btnAgregarF = new javax.swing.JButton();

        setTitle("Administracion Funcion-Pelicula");

        jPanel1.setLayout(new java.awt.BorderLayout());

        jLabel1.setText("Titulo:");
        jPanel2.add(jLabel1);

        txtNombre.setColumns(20);
        jPanel2.add(txtNombre);

        btnAgregar.setText("Agregar");
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });
        jPanel2.add(btnAgregar);

        jPanel1.add(jPanel2, java.awt.BorderLayout.SOUTH);

        lstPeliculas.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(lstPeliculas);

        jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jTabbedPane1.addTab("ALTA DE PELICULA", jPanel1);

        jPanel3.setLayout(new java.awt.BorderLayout());

        jLabel2.setText("Pelicula:");
        jPanel4.add(jLabel2);

        jPanel4.add(cmb);

        btnActualizar.setText("Actualizar");
        btnActualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnActualizarActionPerformed(evt);
            }
        });
        jPanel4.add(btnActualizar);

        jPanel3.add(jPanel4, java.awt.BorderLayout.NORTH);

        jPanel7.setLayout(new java.awt.GridLayout(3, 1));

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("Hora inicio:");
        jPanel7.add(jLabel3);

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("Hora fin:");
        jPanel7.add(jLabel4);

        jLabel5.setText("Entradas disponibles:");
        jPanel7.add(jLabel5);

        jPanel6.add(jPanel7);

        jPanel8.setLayout(new java.awt.GridLayout(3, 1));

        txtInicio.setColumns(5);
        txtInicio.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(java.text.DateFormat.getTimeInstance(java.text.DateFormat.SHORT))));
        txtInicio.setText("00:00");
        jPanel8.add(txtInicio);

        txtFin.setColumns(5);
        txtFin.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(java.text.DateFormat.getTimeInstance(java.text.DateFormat.SHORT))));
        txtFin.setText("00:00");
        jPanel8.add(txtFin);
        jPanel8.add(txtNroEntradas);

        jPanel6.add(jPanel8);

        jPanel5.add(jPanel6);

        jPanel3.add(jPanel5, java.awt.BorderLayout.CENTER);

        btnAgregarF.setText("Agregar");
        btnAgregarF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarFActionPerformed(evt);
            }
        });
        jPanel3.add(btnAgregarF, java.awt.BorderLayout.PAGE_END);

        jTabbedPane1.addTab("ALTA DE FUNCION", jPanel3);

        getContentPane().add(jTabbedPane1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAgregarFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarFActionPerformed
        try {

            IAdministracion i = Fabrica.getIAdministracion();
            
            Double d1 = i.obtenerHoraDouble(txtInicio.getText());
            Double d2 = i.obtenerHoraDouble(txtFin.getText());

            if (d1 < d2) {

                int entradas = Integer.parseInt(txtNroEntradas.getText());
                DTPelicula dtp = (DTPelicula) cmb.getSelectedItem();

                txtInicio.setText("00:00");
                txtFin.setText("00:00");
                txtNroEntradas.setText("");

                i.crearFuncion(new DTFuncion(0, d1, d2, entradas), dtp.getId());

            } else {

                JOptionPane.showMessageDialog(null, "El inicio debe ser anterior al final");
            }

        } catch (NoExistePeliculaException ex) {
            Logger.getLogger(AltaFuncionPelicula.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NumberFormatException nfe) {

            JOptionPane.showMessageDialog(null, "Nro. de entradas debe ser un numero");
        }

    }//GEN-LAST:event_btnAgregarFActionPerformed

    private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed

        IAdministracion i = Fabrica.getIAdministracion();

        String nombre = txtNombre.getText();

        if (nombre.equals("")) {
            JOptionPane.showMessageDialog(null, "Debe ingresar el titulo");

        } else {
            DTPelicula dt = new DTPelicula(0, nombre);
            i.crearPelicula(dt);

            txtNombre.setText("");

            ((DefaultListModel) lstPeliculas.getModel()).addElement(dt);
        }

    }//GEN-LAST:event_btnAgregarActionPerformed

    private void btnActualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnActualizarActionPerformed

        cmb.removeAllItems();

        IKiosco ik = Fabrica.getIKiosco();
        List<DTPelicula> peliculas = ik.listarPeliculas();

        for (DTPelicula dt : peliculas) {
            cmb.addItem(dt);
        }
    }//GEN-LAST:event_btnActualizarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new AltaFuncionPelicula().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnActualizar;
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnAgregarF;
    private javax.swing.JComboBox cmb;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JList lstPeliculas;
    private javax.swing.JFormattedTextField txtFin;
    private javax.swing.JFormattedTextField txtInicio;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtNroEntradas;
    // End of variables declaration//GEN-END:variables
}
